﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing.plaf.basic {
    import org.aswing.*;
    import org.aswing.geom.*;
    import org.aswing.plaf.*;
    import flash.text.*;
    import org.aswing.event.*;
    import org.aswing.graphics.*;
    import flash.ui.*;

    public class BasicButtonUI extends BaseComponentUI {

        private static var viewRect:IntRectangle = new IntRectangle();
        private static var textRect:IntRectangle = new IntRectangle();
        private static var iconRect:IntRectangle = new IntRectangle();

        protected var button:AbstractButton;
        protected var textField:TextField;

        protected function getPropertyPrefix():String{
            return ("Button.");
        }
        override public function installUI(_arg1:Component):void{
            this.button = AbstractButton(_arg1);
            this.installDefaults(this.button);
            this.installComponents(this.button);
            this.installListeners(this.button);
        }
        override public function uninstallUI(_arg1:Component):void{
            this.button = AbstractButton(_arg1);
            this.uninstallDefaults(this.button);
            this.uninstallComponents(this.button);
            this.uninstallListeners(this.button);
        }
        protected function installDefaults(_arg1:AbstractButton):void{
            var _local2:String = this.getPropertyPrefix();
            if (!_arg1.isShiftOffsetSet()){
                _arg1.setShiftOffset(getInt((_local2 + "textShiftOffset")));
                _arg1.setShiftOffsetSet(false);
            };
            if ((_arg1.getMargin() is UIResource)){
                _arg1.setMargin(getInsets((_local2 + "margin")));
            };
            LookAndFeel.installColorsAndFont(_arg1, _local2);
            LookAndFeel.installBorderAndBFDecorators(_arg1, _local2);
            LookAndFeel.installBasicProperties(_arg1, _local2);
            this.button.mouseChildren = false;
            if ((_arg1.getTextFilters() is UIResource)){
                _arg1.setTextFilters(getInstance((_local2 + "textFilters")));
            };
        }
        override public function refreshStyleProperties():void{
            this.installDefaults(this.button);
            this.button.repaint();
            this.button.revalidate();
        }
        protected function uninstallDefaults(_arg1:AbstractButton):void{
            LookAndFeel.uninstallBorderAndBFDecorators(_arg1);
        }
        protected function installComponents(_arg1:AbstractButton):void{
            this.textField = AsWingUtils.createLabel(_arg1, "label");
            _arg1.setFontValidated(false);
        }
        protected function uninstallComponents(_arg1:AbstractButton):void{
            _arg1.removeChild(this.textField);
        }
        protected function installListeners(_arg1:AbstractButton):void{
            _arg1.addStateListener(this.__stateListener);
            _arg1.addEventListener(FocusKeyEvent.FOCUS_KEY_DOWN, this.__onKeyDown);
            _arg1.addEventListener(FocusKeyEvent.FOCUS_KEY_UP, this.__onKeyUp);
        }
        protected function uninstallListeners(_arg1:AbstractButton):void{
            _arg1.removeStateListener(this.__stateListener);
            _arg1.removeEventListener(FocusKeyEvent.FOCUS_KEY_DOWN, this.__onKeyDown);
            _arg1.removeEventListener(FocusKeyEvent.FOCUS_KEY_UP, this.__onKeyUp);
        }
        protected function getTextShiftOffset():int{
            return (this.button.getShiftOffset());
        }
        private function __stateListener(_arg1:AWEvent):void{
            this.button.repaint();
        }
        private function __onKeyDown(_arg1:FocusKeyEvent):void{
            if (!((this.button.isShowing()) && (this.button.isEnabled()))){
                return;
            };
            var _local2:ButtonModel = this.button.getModel();
            if ((((_arg1.keyCode == Keyboard.SPACE)) && (!(((_local2.isRollOver()) && (_local2.isPressed())))))){
                this.setTraversingTrue();
                _local2.setRollOver(true);
                _local2.setArmed(true);
                _local2.setPressed(true);
            };
        }
        private function __onKeyUp(_arg1:FocusKeyEvent):void{
            var _local2:ButtonModel;
            if (!((this.button.isShowing()) && (this.button.isEnabled()))){
                return;
            };
            if (_arg1.keyCode == Keyboard.SPACE){
                _local2 = this.button.getModel();
                this.setTraversingTrue();
                _local2.setPressed(false);
                _local2.setArmed(false);
                _local2.setRollOver(false);
            };
        }
        protected function setTraversingTrue():void{
            var _local1:FocusManager = FocusManager.getManager(this.button.stage);
            if (_local1){
                _local1.setTraversing(true);
            };
        }
        override public function paint(_arg1:Component, _arg2:Graphics2D, _arg3:IntRectangle):void{
            super.paint(_arg1, _arg2, _arg3);
            var _local4:AbstractButton = AbstractButton(_arg1);
            var _local5:Insets = _local4.getMargin();
            if (_local5 != null){
                _arg3 = _local5.getInsideBounds(_arg3);
            };
            viewRect.setRect(_arg3);
            textRect.x = (textRect.y = (textRect.width = (textRect.height = 0)));
            iconRect.x = (iconRect.y = (iconRect.width = (iconRect.height = 0)));
            var _local6:String = AsWingUtils.layoutCompoundLabel(_arg1, _arg1.getFont(), _local4.getDisplayText(), this.getIconToLayout(), _local4.getVerticalAlignment(), _local4.getHorizontalAlignment(), _local4.getVerticalTextPosition(), _local4.getHorizontalTextPosition(), viewRect, iconRect, textRect, ((_local4.getDisplayText() == null)) ? 0 : _local4.getIconTextGap());
            this.paintIcon(_local4, _arg2, iconRect);
            if (((!((_local6 == null))) && (!((_local6 == ""))))){
                this.textField.visible = true;
                if (((_local4.getModel().isArmed()) || (_local4.getModel().isSelected()))){
                    textRect.x = (textRect.x + this.getTextShiftOffset());
                    textRect.y = (textRect.y + this.getTextShiftOffset());
                };
                this.paintText(_local4, textRect, _local6);
            } else {
                this.textField.text = "";
                this.textField.visible = false;
            };
        }
        protected function getIconToLayout():Icon{
            return (this.button.getIcon());
        }
        override protected function paintBackGround(_arg1:Component, _arg2:Graphics2D, _arg3:IntRectangle):void{
        }
        protected function paintText(_arg1:AbstractButton, _arg2:IntRectangle, _arg3:String):void{
            _arg1.bringToTop(this.textField);
            var _local4:ASFont = _arg1.getFont();
            if (this.textField.text != _arg3){
                this.textField.text = _arg3;
            };
            if (!_arg1.isFontValidated()){
                AsWingUtils.applyTextFont(this.textField, _local4);
                _arg1.setFontValidated(true);
            };
            AsWingUtils.applyTextColor(this.textField, this.getTextPaintColor(_arg1));
            this.textField.x = _arg2.x;
            this.textField.y = _arg2.y;
            if (_arg1.getMnemonicIndex() >= 0){
                this.textField.setTextFormat(new TextFormat(null, null, null, null, null, true), _arg1.getMnemonicIndex());
            };
            this.textField.filters = _arg1.getTextFilters();
        }
        protected function getTextPaintColor(_arg1:AbstractButton):ASColor{
            if (_arg1.isEnabled()){
                return (_arg1.getForeground());
            };
            return (BasicGraphicsUtils.getDisabledColor(_arg1));
        }
        protected function paintIcon(_arg1:AbstractButton, _arg2:Graphics2D, _arg3:IntRectangle):void{
            var _local9:Icon;
            var _local4:ButtonModel = _arg1.getModel();
            var _local5:Icon = _arg1.getIcon();
            var _local6:Icon;
            var _local7:Array = this.getIcons();
            var _local8:int;
            while (_local8 < _local7.length) {
                _local9 = _local7[_local8];
                this.setIconVisible(_local9, false);
                _local8++;
            };
            if (_local5 == null){
                return;
            };
            if (!_local4.isEnabled()){
                if (_local4.isSelected()){
                    _local6 = _arg1.getDisabledSelectedIcon();
                } else {
                    _local6 = _arg1.getDisabledIcon();
                };
            } else {
                if (((_local4.isPressed()) && (_local4.isArmed()))){
                    _local6 = _arg1.getPressedIcon();
                } else {
                    if (((_arg1.isRollOverEnabled()) && (_local4.isRollOver()))){
                        if (_local4.isSelected()){
                            _local6 = _arg1.getRollOverSelectedIcon();
                        } else {
                            _local6 = _arg1.getRollOverIcon();
                        };
                    } else {
                        if (_local4.isSelected()){
                            _local6 = _arg1.getSelectedIcon();
                        };
                    };
                };
            };
            if (_local6 != null){
                _local5 = _local6;
            };
            this.setIconVisible(_local5, true);
            if (((_local4.isPressed()) && (_local4.isArmed()))){
                _local5.updateIcon(_arg1, _arg2, (_arg3.x + this.getTextShiftOffset()), (_arg3.y + this.getTextShiftOffset()));
            } else {
                _local5.updateIcon(_arg1, _arg2, _arg3.x, _arg3.y);
            };
        }
        protected function setIconVisible(_arg1:Icon, _arg2:Boolean):void{
            if (_arg1.getDisplay(this.button) != null){
                _arg1.getDisplay(this.button).visible = _arg2;
            };
        }
        protected function getIcons():Array{
            var _local1:Array = new Array();
            if (this.button.getIcon() != null){
                _local1.push(this.button.getIcon());
            };
            if (this.button.getDisabledIcon() != null){
                _local1.push(this.button.getDisabledIcon());
            };
            if (this.button.getSelectedIcon() != null){
                _local1.push(this.button.getSelectedIcon());
            };
            if (this.button.getDisabledSelectedIcon() != null){
                _local1.push(this.button.getDisabledSelectedIcon());
            };
            if (this.button.getRollOverIcon() != null){
                _local1.push(this.button.getRollOverIcon());
            };
            if (this.button.getRollOverSelectedIcon() != null){
                _local1.push(this.button.getRollOverSelectedIcon());
            };
            if (this.button.getPressedIcon() != null){
                _local1.push(this.button.getPressedIcon());
            };
            return (_local1);
        }
        public function getTextField():TextField{
            return (this.textField);
        }
        protected function getButtonPreferredSize(_arg1:AbstractButton, _arg2:Icon, _arg3:String):IntDimension{
            var _local4:IntDimension;
            var _local5:IntRectangle;
            viewRect.setRectXYWH(0, 0, 100000, 100000);
            textRect.x = (textRect.y = (textRect.width = (textRect.height = 0)));
            iconRect.x = (iconRect.y = (iconRect.width = (iconRect.height = 0)));
            AsWingUtils.layoutCompoundLabel(_arg1, _arg1.getFont(), _arg3, _arg2, _arg1.getVerticalAlignment(), _arg1.getHorizontalAlignment(), _arg1.getVerticalTextPosition(), _arg1.getHorizontalTextPosition(), viewRect, iconRect, textRect, ((_arg1.getDisplayText() == null)) ? 0 : _arg1.getIconTextGap());
            if (_arg2 == null){
                _local4 = textRect.getSize();
            } else {
                if ((((_arg1.getDisplayText() == null)) || ((_arg1.getDisplayText() == "")))){
                    _local4 = iconRect.getSize();
                } else {
                    _local5 = iconRect.union(textRect);
                    _local4 = _local5.getSize();
                };
            };
            _local4 = _arg1.getInsets().getOutsideSize(_local4);
            if (_arg1.getMargin() != null){
                _local4 = _arg1.getMargin().getOutsideSize(_local4);
            };
            return (_local4);
        }
        protected function getButtonMinimumSize(_arg1:AbstractButton, _arg2:Icon, _arg3:String):IntDimension{
            var _local4:IntDimension = _arg1.getInsets().getOutsideSize();
            if (_arg1.getMargin() != null){
                _local4 = _arg1.getMargin().getOutsideSize(_local4);
            };
            return (_local4);
        }
        override public function getPreferredSize(_arg1:Component):IntDimension{
            var _local2:AbstractButton = AbstractButton(_arg1);
            return (this.getButtonPreferredSize(_local2, this.getIconToLayout(), _local2.getDisplayText()));
        }
        override public function getMinimumSize(_arg1:Component):IntDimension{
            var _local2:AbstractButton = AbstractButton(_arg1);
            return (this.getButtonMinimumSize(_local2, this.getIconToLayout(), _local2.getDisplayText()));
        }
        override public function getMaximumSize(_arg1:Component):IntDimension{
            return (IntDimension.createBigDimension());
        }

    }
}//package org.aswing.plaf.basic 
